package com.liuqing.www.加密算法.tx加密算法;


/*     */ 
/*     */ import java.io.ByteArrayInputStream;
/*     */ import java.io.ByteArrayOutputStream;
/*     */ import java.io.IOException;
/*     */ import java.io.PrintStream;
/*     */ import java.security.NoSuchAlgorithmException;
/*     */ import java.util.zip.GZIPInputStream;
/*     */ import java.util.zip.GZIPOutputStream;
/*     */ import javax.crypto.Cipher;
/*     */ import javax.crypto.NoSuchPaddingException;
/*     */ import javax.crypto.SecretKey;
/*     */ import javax.crypto.spec.SecretKeySpec;
/*     */ import sun.misc.BASE64Decoder;
/*     */ import sun.misc.BASE64Encoder;
/*     */ 
/*     */ public class DESUtil
/*     */ {
/*  23 */   private String Algorithm = "DESede";
/*  24 */   private byte[] keyBytes = null;
/*     */ 
/* 137 */   private static final char[] digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
/*     */ 
/*     */   public DESUtil(byte[] keyBytes)
/*     */     throws Exception
/*     */   {
/*  28 */     if (keyBytes.length != 24)
/*     */     {
/*  30 */       throw new Exception("the keys's length must be 24!");
/*     */     }
/*  32 */     this.keyBytes = keyBytes;
/*     */   }
/*     */ 
/*     */   private String trimAlgorithm(String alg)
/*     */   {
/*  37 */     int p = alg.indexOf('/');
/*  38 */     if (p == -1) return alg;
/*  39 */     return alg.substring(0, p);
/*     */   }
/*     */ 
/*     */   public DESUtil(byte[] keyBytes, String Algorithm) throws Exception
/*     */   {
/*  44 */     if (keyBytes.length != 24)
/*     */     {
/*  46 */       throw new Exception("the keys's length must be 24!");
/*     */     }
/*     */ 
/*  49 */     this.keyBytes = keyBytes;
/*  50 */     this.Algorithm = Algorithm;
/*     */   }
/*     */ 
/*     */   public byte[] encode(byte[] src)
/*     */   {
/*     */     try
/*     */     {
/*  57 */       byte[] new_src = (byte[])null;
/*     */ 
/*  59 */       int padLen = src.length % 8;
/*  60 */       if (padLen != 0)
/*     */       {
/*  63 */         int new_len = ((src.length - 1) / 8 + 1) * 8;
/*  64 */         new_src = new byte[new_len];
/*  65 */         System.arraycopy(src, 0, new_src, 0, src.length);
/*  66 */         for (int i = 0; i < 8 - padLen; ++i)
/*     */         {
/*  68 */           new_src[(src.length + i)] = 0;
/*     */         }
/*     */       }
/*     */       else
/*     */       {
/*  73 */         new_src = src;
/*     */       }
/*     */ 
/*  76 */       SecretKey deskey = new SecretKeySpec(this.keyBytes, trimAlgorithm(this.Algorithm));
/*     */ 
/*  78 */       Cipher c1 = Cipher.getInstance(this.Algorithm);
/*  79 */       c1.init(1, deskey);
/*  80 */       return c1.doFinal(new_src);
/*     */     }
/*     */     catch (NoSuchAlgorithmException e1)
/*     */     {
/*  84 */       System.out.println("NoSuchAlgorithmException: " + e1);
/*  85 */       return null;
/*     */     }
/*     */     catch (NoSuchPaddingException e2)
/*     */     {
/*  89 */       System.out.println("NoSuchPaddingException: " + e2);
/*  90 */       return null;
/*     */     }
/*     */     catch (Exception e3)
/*     */     {
/*  94 */       System.out.println("Exception: " + e3);
/*  95 */     }return null;
/*     */   }
/*     */ 
/*     */   public byte[] decode(byte[] src)
/*     */   {
/*     */     try
/*     */     {
/* 103 */       SecretKey deskey = new SecretKeySpec(this.keyBytes, trimAlgorithm(this.Algorithm));
/*     */ 
/* 105 */       Cipher c1 = Cipher.getInstance(this.Algorithm);
/* 106 */       c1.init(2, deskey);
/* 107 */       byte[] out = c1.doFinal(src);
/*     */ 
/* 109 */       int zeorNum = 0;
/* 110 */       for (int i = 1; i < out.length; ++i)
/*     */       {
/* 112 */         if (out[(out.length - i)] != 0) continue; ++zeorNum;
/*     */       }
/*     */ 
/* 115 */       byte[] ret = new byte[out.length - zeorNum];
/* 116 */       System.arraycopy(out, 0, ret, 0, ret.length);
/* 117 */       return ret;
/*     */     }
/*     */     catch (NoSuchAlgorithmException e1)
/*     */     {
/* 121 */       System.out.println("NoSuchAlgorithmException: " + e1);
/* 122 */       return null;
/*     */     }
/*     */     catch (NoSuchPaddingException e2)
/*     */     {
/* 126 */       System.out.println("NoSuchPaddingException: " + e2);
/* 127 */       return null;
/*     */     }
/*     */     catch (Exception e3)
/*     */     {
/* 131 */       System.out.println("Exception: " + e3);
/* 132 */     }return null;
/*     */   }
/*     */ 
/*     */   public static String bytes2HexStr(byte[] bytes)
/*     */   {
/* 141 */     if ((bytes == null) || (bytes.length == 0))
/*     */     {
/* 143 */       return null;
/*     */     }
/*     */ 
/* 146 */     char[] buf = new char[2 * bytes.length];
/* 147 */     for (int i = 0; i < bytes.length; ++i)
/*     */     {
/* 149 */       byte b = bytes[i];
/* 150 */       buf[(2 * i + 1)] = digits[(b & 0xF)];
/* 151 */       b = (byte)(b >>> 4);
/* 152 */       buf[(2 * i + 0)] = digits[(b & 0xF)];
/*     */     }
/* 154 */     return new String(buf);
/*     */   }
/*     */ 
/*     */   public static byte[] hexStr2Bytes(String str)
/*     */   {
/* 159 */     if ((str == null) || (str.equals("")))
/*     */     {
/* 161 */       return null;
/*     */     }
/*     */ 
/* 164 */     byte[] bytes = new byte[str.length() / 2];
/* 165 */     for (int i = 0; i < bytes.length; ++i)
/*     */     {
/* 167 */       char high = str.charAt(i * 2);
/* 168 */       char low = str.charAt(i * 2 + 1);
/* 169 */       bytes[i] = (byte)(char2Byte(high) * 16 + char2Byte(low));
/*     */     }
/* 171 */     return bytes;
/*     */   }
/*     */ 
/*     */   public static byte char2Byte(char ch)
/*     */   {
/* 176 */     if ((ch >= '0') && (ch <= '9'))
/*     */     {
/* 178 */       return (byte)(ch - '0');
/*     */     }
/* 180 */     if ((ch >= 'a') && (ch <= 'f'))
/*     */     {
/* 182 */       return (byte)(ch - 'a' + 10);
/*     */     }
/* 184 */     if ((ch >= 'A') && (ch <= 'F'))
/*     */     {
/* 186 */       return (byte)(ch - 'A' + 10);
/*     */     }
/*     */ 
/* 190 */     return 0;
/*     */   }
/*     */ 
/*     */   public static String Bytes2base64Str(byte[] bytes)
/*     */   {
/* 196 */     if ((bytes == null) || (bytes.length == 0))
/*     */     {
/* 198 */       return null;
/*     */     }
/*     */ 
/* 201 */     String out = new BASE64Encoder().encode(bytes);
/* 202 */     String ret = replaceAll(out, "\r\n", "$");
/* 203 */     return ret;
/*     */   }
/*     */ 
/*     */   public static byte[] base64Str2Bytes(String str)
/*     */   {
/* 208 */     if ((str == null) || (str.equals("")))
/*     */     {
/* 210 */       return null;
/*     */     }
/*     */ 
/* 213 */     String in = replaceAll(str, "$", "\r\n");
/*     */     try
/*     */     {
/* 216 */       return new BASE64Decoder().decodeBuffer(in);
/*     */     }
/*     */     catch (Exception e) {
/*     */     }
/* 220 */     return null;
/*     */   }
/*     */ 
/*     */   private static String replaceAll(String s, String src, String dest)
/*     */   {
/* 226 */     if ((s == null) || (src == null) || (dest == null) || (src.length() == 0))
/* 227 */       return s;
/* 228 */     int pos = s.indexOf(src);
/* 229 */     if (pos < 0)
/* 230 */       return s;
/* 231 */     int capacity = (dest.length() > src.length()) ? s.length() * 2 : s.length();
/* 232 */     StringBuilder sb = new StringBuilder(capacity);
/* 233 */     int writen = 0;
/* 234 */     while (pos >= 0)
/*     */     {
/* 236 */       sb.append(s, writen, pos);
/* 237 */       sb.append(dest);
/* 238 */       writen = pos + src.length();
/* 239 */       pos = s.indexOf(src, writen);
/*     */     }
/* 241 */     sb.append(s, writen, s.length());
/* 242 */     return sb.toString();
/*     */   }
/*     */ 
/*     */   public static byte[] compress(byte[] src)
/*     */     throws IOException
/*     */   {
/* 248 */     if ((src == null) || (src.length == 0))
/*     */     {
/* 250 */       return src;
/*     */     }
/*     */ 
/* 253 */     ByteArrayOutputStream out = new ByteArrayOutputStream();
/* 254 */     GZIPOutputStream gzip = new GZIPOutputStream(out);
/* 255 */     gzip.write(src);
/* 256 */     gzip.close();
/* 257 */     return out.toByteArray();
/*     */   }
/*     */ 
/*     */   public static byte[] uncompress(byte[] src)
/*     */     throws IOException
/*     */   {
/* 263 */     if ((src == null) || (src.length == 0))
/*     */     {
/* 265 */       return src;
/*     */     }
/*     */ 
/* 268 */     ByteArrayOutputStream out = new ByteArrayOutputStream();
/* 269 */     ByteArrayInputStream in = new ByteArrayInputStream(src);
/* 270 */     GZIPInputStream gunzip = new GZIPInputStream(in);
/* 271 */     byte[] buffer = new byte[256];
/*     */     int n;
/* 273 */     while ((n = gunzip.read(buffer)) >= 0)
/*     */     {
/* 275 */       out.write(buffer, 0, n);
/*     */     }
/*     */ 
/* 278 */     gunzip.close();
/* 279 */     return out.toByteArray();
/*     */   }
/*     */ 
/*     */   public static void main(String[] args)
/*     */   {
/* 284 */     String linkId = "1234567";
/* 285 */     int cpId = 1;
/* 286 */     short gameId = 2;
/* 287 */     String uid = "9e41aedac5bf11ddb84c0007e9242eff";
/* 288 */     String retUrl = "http://cw6.3g.qq.com/index.jsp?sid=h6IhdFi7D2CY7tF/kqGNhg==&hitems=&items=";
/* 289 */     short goodsId = 10;
/* 290 */     int goodsCount = 1;
/* 291 */     int timeStamp = (int)(System.currentTimeMillis() / 1000L);
/*     */ 
/* 293 */     String ss = linkId + "|" + cpId + "|" + gameId + "|" + uid + "|" + retUrl + "|" + goodsId + "|" + goodsCount + "|" + timeStamp;
/*     */ 
/* 295 */     int tester = 2;
/* 296 */     if (1 != tester)
/*     */       return;
/* 298 */     testDesCompressBase64(ss);
/*     */   }
/*     */ 
/*     */   private static void testDesCompressBase64(String ss)
/*     */   {
/* 308 */     System.out.println(ss);
/*     */     try
/*     */     {
/* 311 */       byte[] key = "123456789012345678901234".getBytes();
/* 312 */       System.out.println("len 0 :   " + ss.length());
/*     */ 
/* 314 */       byte[] info = (byte[])null;
/*     */       try
/*     */       {
/* 317 */         info = new DESUtil(key, "DESede/ECB/NoPadding").encode(ss.getBytes("UTF-8"));
/*     */       }
/*     */       catch (Exception e)
/*     */       {
/* 322 */         e.printStackTrace();
/*     */       }
/*     */ 
/* 325 */       System.out.println("len 1 :   " + info.length);
/*     */ 
/* 327 */       byte[] out = compress(info);
/* 328 */       System.out.println("len 2 :   " + out.length);
/*     */ 
/* 330 */       String str_info = Bytes2base64Str(out);
/* 331 */       System.out.println("len 3 :   " + str_info.length());
/*     */ 
/* 333 */       System.out.println(str_info);
/*     */ 
/* 335 */       byte[] zipBytes = base64Str2Bytes(str_info);
/*     */ 
/* 337 */       byte[] oout = uncompress(zipBytes);
/*     */ 
/* 339 */       byte[] encryptedInfoBytes = (byte[])null;
/*     */       try
/*     */       {
/* 342 */         encryptedInfoBytes = new DESUtil(key, "DESede/ECB/NoPadding").decode(oout);
/*     */       }
/*     */       catch (Exception e)
/*     */       {
/* 346 */         e.printStackTrace();
/*     */       }
/*     */ 
/* 349 */       System.out.println(new String(encryptedInfoBytes));
/*     */     }
/*     */     catch (IOException e)
/*     */     {
/* 353 */       e.printStackTrace();
/*     */     }
/*     */   }
/*     */ }

/* Location:           C:\Documents and Settings\韩世超\桌面\NetGameApp_chuhan.jar
 * Qualified Name:     com.tencent.wapgame.netgame.util.DESUtil
 * JD-Core Version:    0.5.4
 */